package com.movie.mybatis.web;

import com.movie.mybatis.service.impl.LoginAndExitServlet;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet({"/welcome","/login","/exit"})
public class LoginAndExitController extends HttpServlet {
    private final LoginAndExitServlet userService = new LoginAndExitServlet();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String servletPath = request.getServletPath();

        String jump = ("1".equals(request.getParameter("d"))) ? "root" : "record";

        if ("/welcome".equals(servletPath)) {
            userService.CookieJudge(request, response);
        } else if ("/login".equals(servletPath)) {
            boolean success = userService.Login(request, jump);
            if (success){
                String f = request.getParameter("f");
                if ("1".equals(f) && jump.equals("record")){
                    userService.NoLoginJudgment(request, response);
                }
                response.sendRedirect(request.getContextPath()+"/"+jump+"/list");
            }
        }else if ("/exit".equals(servletPath)){
            userService.Exit(request, response);
        }
    }
}
